<form method="POST" onSubmit="onSubmit(); return false;">
    <div class="row-fluid">
        <div class="span2">
            <label for="action">任务类型</label>
        </div> <!-- .span2 -->
        <div class="span8">
            <div class="row-fluid">
                <div class="span6">
                    <input id="action" name="action" type="radio" value="import" checked />导入
                </div> <!-- .span6 -->
                <div class="span6">
                    <input id="action" name="action" type="radio" value="export" />导出
                </div> <!-- .span6 -->
            </div> <!-- .row-fluid -->
        </div> <!-- .span8 -->
    </div> <!-- .row-fluid -->
    <div class="row-fluid">
        <div class="span2">
            <label for="ip-format">导入/导出格式</label>
        </div> <!-- .span2 -->
        <div class="span10">
            <select id="ip-format">
                <option value="1" selected>IP|IP|IP (Gogo-Tester/Goagent格式)</option>
                <option value="2">换行分割</option>
                <option value="3">智能正则匹配</option>
            </select>
        </div> <!-- .span10 -->
    </div> <!-- .row-fluid -->
    <div class="row-fluid">
        <div class="span2">
            <label for="ip-list">IP列表</label>
        </div> <!-- .span2 -->
        <div class="span10">
            <textarea id="ip-list" rows="10"></textarea>
        </div> <!-- .span10 -->
    </div> <!-- .row-fluid -->

    <div class="row-fluid">
        <div class="span12">
            <button class="btn btn-primary btn-block" type="submit">执行</button>
        </div> <!-- .span12 -->
    </div> <!-- .row-fluid -->
</form> <!-- #import-export-ip -->

<!-- JavaScript -->
<script type="text/javascript">
    title('IP导入导出');
</script>
<script type="text/javascript">
    function onSubmit() {
        var action   = $('#action:checked').val(),
            ipFormat = $('#ip-format').val(),
            ipList   = $('#ip-list').val();

        if ( action == 'import' ) {
            return doImportAction(ipFormat, ipList);
        } else if ( action == 'export' ) {
            return doExportAction(ipFormat, ipList);
        }
    }
</script>
<script type="text/javascript">
    $('input[name="action"]').click(onActionChange);

    function onActionChange() {
        var action = $('#action:checked').val();

        if( action == 'import' ) {
            $('option[value=1]').css('display', 'none');
            $('option[value=2]').css('display', 'none');
            $('option[value=3]').css('display', '');
            $('#ip-format').val("3")
        } else if ( action == 'export' ) {
            $('option[value=1]').css('display', '');
            $('option[value=2]').css('display', '');
            $('option[value=3]').css('display', 'none');
            
            if( $('#ip-format').val() == 3 ) {
                $('#ip-format').val("1");
            }
        }
    }

    onActionChange();
</script>
<script type="text/javascript">
    function doImportAction(ipFormat, ipList) {
        ipList = conversionFormat(ipFormat, '1', ipList);
        console.log('尝试导入 ', ipList.split('|').length, ' 个IP.');

        var postData = {
            'ipList': ipList
        };

        $.ajax({
            type: 'POST',
            url: 'http://127.0.0.1:8084/importip?cmd=importip',
            data: postData,
            dataType: 'JSON',
            success: function(result) {
                tip('成功导入' + result['res'] + '个IP.', 'success');
            },
            error: function() {
                tip('导入IP失败');
            }
        });
    }
</script>
<script type="text/javascript">
    function doExportAction(ipFormat, ipList) {
        $.ajax({
            type: 'POST',
            url: 'http://127.0.0.1:8084/importip?cmd=exportip',
            dataType: 'JSON',
            success: function(result) {
                var ipList = result['res'];

                tip('成功导出' + ipList.split('|').length + '个IP.', 'success');

                ipList = conversionFormat('1', ipFormat, ipList);
                $('#ip-list').val(ipList);
            },
            error: function() {
                tip('导出IP失败.', 'error');
            }
        });
    }
</script>
<script type="text/javascript">
    function conversionFormat(originalFormat, targetFormat, sourceData) {
        if( originalFormat == targetFormat ) {
            return sourceData;
        }

        var ipList;
        switch(originalFormat) { //由源数据转换为中间格式
        case '1': //IP|IP|IP
            ipList = sourceData.split("|");
            break;
        case '2': //换行分割
            ipList = sourceData.split("\n");
            break;
        case '3': //智能正则匹配
            var tmp = ' ' + sourceData;
            var regex = /(?!\d)[^\d](([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])(\.([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])){3}(?!\d))/gm;

            var regexResult;
            ipList = [];
            while(true) {
                regexResult = regex.exec(tmp);

                if(regexResult === null) {
                    break;
                }

                ipList.push(regexResult[1]);
            }
            break;
        }

        var result;
        switch(targetFormat) { //由中间格式转换为目标格式
        case '1': //IP|IP|IP
            result = '';
            for(var id in ipList) {
                result += ipList[id] + '|';
            }
            result = result.substr(0, result.length - 1);
            break;
        case '2': //换行分割
            result = '';
            for(var id in ipList) {
                result += ipList[id] + '\n';
            }
            break;
        }

        return result;
    }
</script>